package pxene.test.logicregression

import org.apache.log4j.{ Level, Logger }
import org.apache.spark.{ SparkContext, SparkConf }
import org.apache.spark.mllib.regression.LinearRegressionWithSGD
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LinearRegressionModel
import org.apache.spark.mllib.classification.LogisticRegressionWithLBFGS
import org.apache.spark.mllib.classification.LogisticRegressionModel

object LogicRegressionModelUse {
  def main(args: Array[String]): Unit = {
    // 屏蔽不必要的日志显示终端上
    Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
    Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)

    // 设置运行环境
    val conf = new SparkConf().setAppName("regression").setMaster("local[4]")
    val sc = new SparkContext(conf)

    // Load and parse the data
    val data = sc.textFile("file:///home/chenjinghui/regression/regression_use.txt")
    val parsedData = data.map { line =>
      val parts = line.split(',')
      (parts(0), Vectors.dense(parts(1).split(' ').map(_.toDouble)))
    }

    val model = LogisticRegressionModel.load(sc, "file:///home/chenjinghui/regression/model")
    val labelAndPreds = parsedData.map { point =>
      val prediction = model.predict(point._2)
      (point._1, prediction)
    }
    
    labelAndPreds.saveAsTextFile("file:///home/chenjinghui/regression/result")

  }
}